package com.wpr.shop.modules.global.vo;

import com.wpr.shop.utils.JsonUtil;
import com.wpr.shop.modules.global.validate.ErrorCode;

/**
 * describe:
 *
 * @author zhanglisheng
 * @date 2018/10/24
 */
public class CommonResp<T> {

    private Meta  meta;

    protected T data;

    /**
     * 判断返回结果是否成功
     */
    public boolean success() {
        return meta.ecode == 0;
    }

    /**
     * construct
     */
    public CommonResp() {
        meta = new Meta();
    }

    public CommonResp(T data) {
        this.data = data;
        meta = new Meta();
    }

    public CommonResp(ErrorCode errorCode, T data) {
        meta = new Meta(errorCode.getValue(), errorCode.getComment());
        this.data = data;
    }

    public CommonResp(int ecode, String emsg) {
        meta = new Meta(ecode, emsg);
    }

    public CommonResp(int ecode, String emsg, T data) {
        meta = new Meta(ecode, emsg);
        this.data = data;
    }

    public static class Meta{
        private int ecode=0;
        private String emsg="成功";

        /**
         * construct
         */
        public Meta(){};

        public Meta(int ecode,String emsg){
            this.ecode = ecode;
            this.emsg = emsg;
        }

        public int getEcode() {
            return ecode;
        }

        public void setEcode(int ecode) {
            this.ecode = ecode;
        }

        public String getEmsg() {
            return emsg;
        }

        public void setEmsg(String emsg) {
            this.emsg = emsg;
        }

        public String toString(){
            return JsonUtil.obj2Json(this);
        }


    }

    public Meta getMeta() {
        return meta;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    @Override
    public String toString() {
        return JsonUtil.obj2Json(this);
    }
}
